package towa.com.remoteWMP;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class SettingsActivity extends Activity {

	private static final String PREFS_NAME = "WMPRemotePrefs";
	private SettingsDataSource dataSource;
	private HostArrayAdapter adapter;
	private List<Host> hosts;
	private Host hostSelected;
	private Button add;
	private Button update;
	private Button remove;
	private Button connect;
	private EditText nameInput;
	private EditText ipInput;
	private EditText portInput;
	private ListView list;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.hostlayout);
		dataSource = new SettingsDataSource(this);
		dataSource.open();
		
		hosts = dataSource.getAllHosts();
		adapter = new HostArrayAdapter(this,hosts);
		list = (ListView) findViewById(R.id.listView1);
		list.setAdapter(adapter);
		list.setOnItemClickListener(listListener);
		
		add = (Button) findViewById(R.id.addButton);
		add.setOnClickListener(buttonListener);
		update = (Button) findViewById(R.id.updateButton);
		update.setOnClickListener(buttonListener);
		remove = (Button) findViewById(R.id.removeButton);
		remove.setOnClickListener(buttonListener);
		connect = (Button) findViewById(R.id.connectButton);
		connect.setOnClickListener(buttonListener);
		nameInput = (EditText) findViewById(R.id.hostNameInput);
		ipInput = (EditText) findViewById(R.id.ipInput);
		portInput = (EditText) findViewById(R.id.portInput);
	}
		
	OnItemClickListener listListener = new OnItemClickListener(){

		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			hostSelected = (Host)arg0.getItemAtPosition(arg2);
			updateFieldsWithHost(hostSelected);
		}
	
	};
		
	OnClickListener buttonListener = new OnClickListener(){

		public void onClick(View v) {
			if(v.getId() ==  add.getId()){
				long id = dataSource.createHost(getHostFromFields());
				Host h = dataSource.getHost(id);
				if(h != null)
					hostSelected = h;
			}else if(v.getId() == update.getId()){
				Host h = getHostFromFields();
				if(hostSelected != null){
					h.setId(hostSelected.getId());
					dataSource.updateHost(h);
					hostSelected = h;
				}
			}else if(v.getId() == remove.getId()){
				if(hostSelected != null){
					dataSource.deleteHost(hostSelected);
					hostSelected = null;
					updateFieldsWithHost(new Host());
				}
			}else if(v.getId() == connect.getId()){
				SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
				SharedPreferences.Editor editor = settings.edit();
				
				Host h = getHostFromFields();
				editor.putString("address", h.getIp());
				editor.putInt("port", h.getPort());
				editor.commit();
				
				Intent intent = new Intent(SettingsActivity.this, RemoteWMPActivity.class);
				intent.putExtra("address", h.getId());
				intent.putExtra("port", h.getPort());
				setResult(Activity.RESULT_OK, intent);
				finish();
			}
			hosts = dataSource.getAllHosts();
			adapter = new HostArrayAdapter(SettingsActivity.this, hosts);
			list.setAdapter(adapter);
//			HostArrayAdapter a = (HostArrayAdapter) list.getAdapter();
//			a.notifyDataSetChanged();
			adapter.notifyDataSetChanged();
		}
		
	};
	
	private Host getHostFromFields(){
		Host h = new Host();
		h.setName(nameInput.getText().toString());
		h.setIp(ipInput.getText().toString());
		String temp = portInput.getText().toString();
		int port = 0;
		try {
			port = Integer.parseInt(temp);
		} catch(Exception e){}
		h.setPort(port);
		return h;
	}
	
	private void updateFieldsWithHost(Host h){
		nameInput.setText(h.getName());
		ipInput.setText(h.getIp());
		portInput.setText("" + h.getPort());
	}
	
	@Override
	public void onResume(){
		super.onResume();
		dataSource.open();
	}
	
	@Override
	public void onPause(){
		super.onPause();
		dataSource.close();
	}
	
}
